layui.use(['layer', 'table','element'], function () {
    var layer = layui.layer
        , $ = layui.$
        , table = layui.table //表格
        ,element = layui.element;
    $('.btnSearch').on('click', function () {
        renderTable();
    })
    $('.btnClear').on('click', function () {
        $("[name='groovy-name']").val('');
        renderTableNull();
    })
    $('.addForm').on('click', function () {
        setLineData('','','','','','');
        layer.open({
            type: 2
            , title: '新建脚本'
            , content: 'addScript'
            , area: ['90%', '90%']
            , yes: function (index, layero) {
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            },
            success: function (layero, index) {
                $(".layui-layer-title")[0].innerText = '新建脚本';
            },
        });
    })
    $('.addService').on('click', function () {
        setLineData('','','','','','');
        layer.open({
            type: 2
            , title: '添加接口'
            , content: 'addService'
            , area: ['90%', '90%']
            , yes: function (index, layero) {
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            },
            success: function (layero, index) {
                $(".layui-layer-title")[0].innerText = '添加接口';
            },
        });
    })
    $('.uploadForm').on('click', function () {
        layer.open({
            type: 2
            , title: '上传脚本'
            , content: 'uploadScript'
            , area: ['90%', '90%']
            , yes: function (index, layero) {
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            },
            success: function (layero, index) {
                $(".layui-layer-title")[0].innerText = '上传脚本';
            },
        });
    })
    $('.logButton').on('click', function () {
        layer.open({
            type: 2
            , title: '历史记录'
            , content: 'logPage'
            , area: ['90%', '90%']
            , yes: function (index, layero) {
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            },
            success: function (layero, index) {
                $(".layui-layer-title")[0].innerText = '历史记录';
            },
        });
    })
    element.on('tab(docDemoTabBrief)', function(data){
        renderTable();
    });
    table.on('tool(faultTable)', function(obj){
        var lineData = obj.data;
        if (obj.event === 'detail') {
            if (lineData.gstate==0){
                layer.alert('运行中不可编辑！');
                return;
            }
            setLineData(lineData.gid,lineData.gname,lineData.gurl,lineData.gpath,lineData.gmethod,lineData.gdescribe);
            layer.open({
                type: 2
                , content: 'addService'
                , area: ['90%', '90%']
                , yes: function (index, layero) {
                    //点击确认触发 iframe 内容中的按钮提交
                    // var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    // submit.click();
                },
                success: function (layero, index) {
                    $(".layui-layer-title")[0].innerText =  '接口编辑';
                },
            });
        }
        if (obj.event === 'scriptdetail') {
            setLineData(lineData.gid,lineData.gname,null,lineData.gpath,null,null);
            layer.open({
                type: 2
                , content: 'addScript'
                , area: ['90%', '90%']
                , yes: function (index, layero) {
                    //点击确认触发 iframe 内容中的按钮提交
                    // var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    // submit.click();
                },
                success: function (layero, index) {
                    $(".layui-layer-title")[0].innerText =  '接口编辑';
                },
            });
        }
        if (obj.event.indexOf('eventCheck_') > -1) {
            var indexConfig = obj.event.split('_')[1];
            $.ajax({
                url: '/groovy/scriptState',
                type: 'post',
                data: { id:lineData.gid,path: lineData.gpath, method: lineData.gmethod, service: lineData.gurl,state:indexConfig},
                contentType: 'application/x-www-form-urlencoded',
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, { time: 900, icon: 6 }, function () {
                            renderTable();
                        });//失败的表情5 成功的表情6
                        return;
                    } else {
                        layer.msg(data.msg, { icon: 5 });//失败的表情5 成功的表情6
                        return;
                    }
                },
            });
        }
        if (obj.event.indexOf('delete_') > -1) {
            var indexConfig = obj.event.split('_')[1];
            layer.open({
                type: 1
                ,offset: "offset"
                ,id: 'layerDemo' //防止重复弹出
                ,content: '<div style="padding: 20px 100px;">是否确认删除</div>'
                ,btn: '删除'
                ,btnAlign: 'c' //按钮居中
                ,shade: 0 //不显示遮罩
                ,yes: function(){
                    $.ajax({
                        url: '/groovy/delete'+indexConfig,
                        type: 'post',
                        data: { id:lineData.gid,name:lineData.gname,path:lineData.gpath},
                        contentType: 'application/x-www-form-urlencoded',
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.msg, { time: 900, icon: 6 }, function () {
                                    renderTable();
                                });//失败的表情5 成功的表情6
                                return;
                            } else {
                                layer.msg(data.msg, { icon: 5 });//失败的表情5 成功的表情6
                                return;
                            }
                        },
                    });
                    layer.closeAll();
                }
            });
        }
    });
    window.renderTable=function () {
        var groovyName = $("[name='groovy-name']").val();
        if (document.getElementById("serverList").classList.length > 0) {
            table.render({
                elem: '#test'
                , url: '/groovy/search'
                , where: { 'taskName': groovyName}
                , parseData: function (res) {
                    return {
                        "code": res.code==200?0:res.code,
                        "msg": res.msg,
                        "count": res.total,
                        "data": res.rows
                    };
                }
                , cols: [[
                    { field: 'gname', title: '脚本名称', align: 'center', width: '10%' }
                    , { field: 'gpath', title: '脚本路径', align: 'center', width: '15%' }
                    , { field: 'gservice', title: '访问路径', align: 'center', width: '20%' }
                    , { field: 'gmethod', title: '调用方法', align: 'center', width: '10%' }
                    , { field: 'gdescribe', title: '脚本描述', align: 'center', width: '15%' }
                    , { field: 'right', title: '控制',align: 'center', templet: generateScriptBtn}
                ]]
                , page: true
            });
        }else{
            table.render({
                elem: '#test'
                , url: '/groovy/searchScript'
                , where: { 'taskName': groovyName}
                , parseData: function (res) {
                    return {
                        "code": res.code==200?0:res.code,
                        "msg": res.msg,
                        "count": res.total,
                        "data": res.rows
                    };
                }
                , cols: [[
                    { field: 'gname', title: '脚本名称', align: 'center', width: '33%' }
                    , { field: 'gpath', title: '脚本路径', align: 'center', width: '33%' }
                    , { field: 'right', title: '控制',align: 'center', templet: generateScript}
                ]]
                , page: true
            });
        }
    }
    window.renderTableNull=function () {
        table.render({
            elem: '#test'
            , data: []
            , cols: [[
                { field: 'gname', title: '脚本名称', align: 'center', width: '10%' }
                , { field: 'gpath', title: '脚本路径', align: 'center', width: '15%' }
                , { field: 'gservice', title: '访问路径', align: 'center', width: '20%' }
                , { field: 'gmethod', title: '调用方法', align: 'center', width: '10%' }
                , { field: 'gdescribe', title: '脚本描述', align: 'center', width: '15%' }
                , { field: 'right', title: '控制',align: 'center', templet: generateScriptBtn}
            ]]
            , page: true
        })
    }
    renderTable();
});
function generateScriptBtn(data) {
    var htmlStr = "";
    if (data.gstate == 0) {//0:启动
        htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="eventCheck_start" script_value="' + data.gservice + '" style="background-color:#FF5722;opacity: 0.2">启动</a>';
        htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="eventCheck_stop" script_value="' + data.gservice + '">暂停</a>';
    } else {
        htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="eventCheck_start" script_value="' + data.gservice + '">启动</a>';
        htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="eventCheck_stop" script_value="' + data.gservice + '" style="background-color:#FF5722;opacity: 0.2">暂停</a>';
    }
    htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="detail" script_value="' + data.gservice + '">编辑</a>';
    htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="delete_Service" script_value="' + data.gservice + '" style="background-color:#FF5722;">删除</a>';
    return htmlStr;
}
function generateScript(data) {
    var htmlStr = "";
    htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="scriptdetail" script_value="' + data.gpath + '">编辑</a>';
    htmlStr += '<a class="layui-btn layui-btn-sm" lay-event="delete_Script" script_value="' + data.gservice + '" style="background-color:#FF5722;">删除</a>';
    return htmlStr;
}